<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>基础知识</title>
	<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css"" rel="stylesheet">
	<link href="https://cdn.bootcdn.net/ajax/libs/animate.css/3.2.3/animate.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="../../static/css/public.css" />
	<style type="text/css">
		li{
			text-indent: 3em;
			list-style: none;
		}
	</style>
</head>
<body>
	<!-- 回到顶部 -->
	<div class="topBtn">
		<a href="#top">
			<i class="fa fa-long-arrow-up"></i>
		</a>
	</div>
	<!--右边内容-->
	<div id="app" class="main animated fadeInRight">
		<div class="shadow">
			<div class="Table" v-cloak id="top">
				<div class="TableRow" v-for="(el,index) in list">
					<a class="RowLeft" :href="'#'+el.id">{{el.name}}</a>
					<span class="RowRight">{{el.explain}}</span>
				</div>
			</div>
			<div>
				<h4>原生Ajax请求</h4>
				<pre>
	var xhr=new XMLHttpRequest();
	console.log("当前的状态值",xhr.readyState);//output:0  放到open后输出为1
	xhr.addEventListener("readystatechange",function(e){
		console.log("当前的状态值",xhr.readyState);
		=>output:1 2 3 4 
		if (xhr.readyState==4 && e.target.status==200){  
			xhr.readyState等价e.target.readyState
			console.log("下载完成");
			console.log(typeof e.target.response);
			=>output:string		网络基本上都是字符串形式传输
			renderDate(e.target.response); //调用函数
			console.log(JSON.parse(e.target.response));
		}
	})
	2.发送请求 open 方法
	xhr.open("GET","https://www.easy-mock.com/mock/5baafee49f43b479fb7c86ac/getShopList");
	//设置请求头
	xhr.setRequestHeader('content-type', 'application/json;charset=utf-8')//设置文件格式
	// 'content-type'	text/html;text/plain(纯文本);
	xhr.setRequestHeader('dataType', 'json')// 当请求本地json数据的时候	'./data.json'
	xhr.setRequestHeader('Cache-Control', 'no-cache')//设置请求是否缓存
	将请求发送出去send方法
	xhr.send();
	4.处理数据
	xhr.addEventListener("readystatechange",function(e){
		console.log(e.target.response);
	})
	function renderDate(responce){
		var baba=document.querySelector(".baba");
		var dataObj=JSON.parse(responce);
		var template=function(pic,title,content,zanTotal){
			return `&lt;div class="pic-wrap"&gt;
					&lt;img src=${pic} /&gt;
					&lt;/div&gt;
					&lt;div class="content"&gt;
						&lt;h4&gt;${title}&lt;/h4&gt;
						&lt;p&gt;${content}&lt;/p&gt;
						&lt;p&gt;${zanTotal}人说好&lt;/p&gt;
					&lt;/div&gt;`;
		}
		dataObj.result.forEach(function(el,index){
			var item=document.createElement("div");
			item.classList.add("item");
			item.innerHTML=template(el.pic,el.title,el.content,el.zanTotal);
			baba.appendChild(item);
		})
	}				
				</pre>
			</div>
			<div>
				<h4 id="Promise">Promise封装</div>
				<pre>
	// 封装一个get请求的方法
	function HttpRequest(url,data) {
		return new Promise(function (resolve, reject) {
			var XHR = XMLHttpRequest ? new XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP');
			XHR.onreadystatechange = function () {
				//readyState属性表示请求/响应过程的当前活动阶段。
				if (XHR.readyState == 4) {
					if ((XHR.status >= 200 && XHR.status &lt; 300) ) {
						var response = JSON.parse(XHR.responseText);
						resolve(response);
					} else {
						//reject(new Error("Request was unsuccessful: " + XHR.statusText));
						var err = JSON.parse(XHR.responseText);
						reject(err);
					}
				}
			}
			XHR.open('GET', url , true);
			XHR.send(data);
		})
	}
	
	HttpRequest(url,data).then(res => console.log(res))
		.catch((err)=>{
			console.log(err)
		})
				</pre>
			</div>
			<div id="status">
				<div class="code">状态值、状态码</div>
				<div>状态值:</div>			  
				<ul>
					<li>0		已创建XMLHttpRequest对象,但未调用open</li>
					<li>1		已调用open,未send</li>
					<li>2		已调用send,可以访问头部和状态</li>
					<li>3		下载中</li>
					<li>4		下载完成,前端可以处理数据</li>
				</ul>
				<div>状态码:</div>	
				<ul>
					<li>1xx	接收到请求并继续处理</li>
					<li>2xx	处理成功响应类</li>
					<li>3xx	重定向响应类</li>
					<li>4xx	客户端错误,语法错误,有些语句不能正确执行</li>
					<li>5xx	服务端问题</li>
				</ul>
			</div>
		</div>
	</div>
	<!--页面加载进度条-->
	<script src="https://cdn.bootcdn.net/ajax/libs/pace/0.5.0/pace.min.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.17/vue.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var app = new Vue({
			el: "#app",
			data: function () {
				return {
					list:[
						{name:"原生Ajax请求",id:"ajax",explain:''},
						{name:"Promise封装",id:"Promise",explain:''},
						{name:"状态值、状态码",id:"status",explain:''},
						// {name:"find()",id:"find",explain:'过滤,'},
						// {name:"every()",id:"every",explain:'只要一个不符合条件，返回false'},
						// {name:"some()",id:"some",explain:'只有一个符合条件，返回true'},
						// {name:"map",id:"map",explain:''},
						// {name:"遍历按顺序执行",id:"await",explain:'让遍历可以按顺序执行'},
					]
				}
			},
		})
	</script>
</body>

</html>